package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingArticleCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingArticlePreviewDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingArticleUpdateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingMaterialUpdateDTO;
import com.pactera.madp.cp.api.entity.BaseEntity;
import com.pactera.madp.cp.api.entity.marketing.MarketingArticleEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingArticleVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.MarketingArticleService;
import com.pactera.madp.cp.service.MarketingHotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (McMarketingArticle)表控制层
 *
 * @author yushen.qi
 * @since 2021-05-24 15:30:55
 */
@RestController
@RequestMapping("/marketing-article")
@Api(value = "marketingArticle", tags = "营销素材-文章管理")
public class MarketingArticleController {

    /**
     * 服务对象
     */
    @Resource
    private MarketingArticleService marketingArticleService;
    @Resource
    private MarketingHotService marketingHotService;

    /**
     * 模糊查询
     *
     * @param title
     * @param classification
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "文章标题模糊查询")
    public R<List<MarketingArticleVO>> qryPage (String title, Integer classification) {
        return R.ok(marketingArticleService.selectAllOrName(title,classification));
    }

    /**
     * 新增文章
     *
     * @param dto
     * @return
     */
    @SysLog("新增营销素材-文章")
    @PostMapping
    @ApiOperation(value = "新增文章")
    public R<Map<String, Integer>> saveArticle(@RequestBody MarketingArticleCreateDTO dto){
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingArticleEntity one = marketingArticleService.getOne(Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getTitle, dto.getTitle()).eq(BaseEntity::getDelFlag,0).eq(MarketingArticleEntity::getCorpId,corpId).eq(MarketingArticleEntity::getClassification,dto.getClassification()));
        if(one!=null){
            return R.failed("文章标题不能重复！");
        }
        return R.ok(marketingArticleService.insert(dto));
    }

    /**
     * 编辑文章
     *
     * @param marketingArticleUpdateDto
     * @return
     */
    @SysLog("编辑营销素材-文章")
    @PutMapping
    @ApiOperation(value = "编辑文章")
    public R<Boolean> putArticle(@RequestBody MarketingArticleUpdateDTO marketingArticleUpdateDto){
      return marketingArticleService.update(marketingArticleUpdateDto)?R.ok():R.failed("修改失败");
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @SysLog("删除营销素材-文章")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除文章")
    public R delete(@PathVariable Integer id){
        return  marketingArticleService.remove(id);
    }

    /**
     * 发布文章
     *
     * @param id
     * @return
     */
    @SysLog("发布营销素材-文章")
    @GetMapping("/release/{id}")
    @ApiOperation(value = "发布文章")
    public R<Boolean> releaseArticle(@PathVariable Integer id){
               MarketingArticleEntity marketingArticleEntity = new MarketingArticleEntity();
               marketingArticleEntity.setMarketingState(2);
      return marketingArticleService.update(marketingArticleEntity,Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId,id))?R.ok(null,"发布成功"):R.failed("发布失败");
    }

    /**
     * 预览文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/preview")
    @ApiOperation(value = "预览接口")
    public R previewArticle(@RequestBody MarketingArticlePreviewDTO dto){
     return marketingArticleService.previewArticle(dto);
    }

    @SysLog("保存选中的文章素材")
    @PostMapping("/post/material")
    @ApiOperation(value = "保存选中的文章素材")
    public R saveMarketing(@RequestBody MarketingMaterialUpdateDTO dto){
        return marketingArticleService.saveMaterial(dto);
    }

    @SysLog("保存并发布选中的文章素材")
    @PutMapping("/put/material")
    @ApiOperation(value = "保存并发布选中的文章素材")
    public R put(@RequestBody MarketingMaterialUpdateDTO dto){
        return marketingArticleService.updateMaterial(dto);
    }

    @GetMapping("/material")
    @ApiOperation(value = "标题模糊/查询当前公众号的素材")
    public R getMaterialList(String appId,String title,Integer classification){
        return R.ok(marketingArticleService.selectMaterial(appId, title, classification));
    }

}
